<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>新建目录</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.lstat.html">« lstat</a></li>
      <li style="float: right;"><a href="function.move-uploaded-file.html">move_uploaded_file »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>新建目录</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mkdir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mkdir</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mkdir</span> &mdash; <span class="dc-title">新建目录</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.mkdir-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mkdir</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$permissions</code><span class="initializer"> = 0777</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">bool</span> <code class="parameter">$recursive</code><span class="initializer"> = <strong><code>false</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   尝试新建由 <code class="parameter">directory</code> 指定的目录。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mkdir-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">directory</code></dt>

     <dd>

      <p class="para">
       目录的路径。<div class="tip"><strong class="tip">小技巧</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">permissions</code></dt>

     <dd>

      <p class="para">
       默认权限是 0777，意味着最大可能的访问权。有关权限的更多信息请阅读 <span class="function"><a href="function.chmod.html" class="function">chmod()</a></span> 页面。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        <code class="parameter">permissions</code> 在 Windows 下被忽略。
       </p>
      </p></blockquote>
      <p class="para">
       注意也许想用八进制数指定
       <code class="parameter">permissions</code>，也就是说该数应以零打头。<code class="parameter">permissions</code>
       也会被当前的 umask 修改，可以用 <span class="function"><a href="function.umask.html" class="function">umask()</a></span> 来改变。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">recursive</code></dt>

     <dd>

      <p class="para">
       如果为 <strong><code>true</code></strong>，还将会创建指定 <code class="parameter">directory</code> 的任何父级目录，并具有相同的权限。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para"><a href="stream.contexts.html" class="link">上下文流（context stream）</a>
<span class="type">resource</span>。</p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.mkdir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果创建的目录已存在，则视为错误，仍然返回 <strong><code>false</code></strong>。在尝试创建之前，使用
    <span class="function"><a href="function.is-dir.html" class="function">is_dir()</a></span> 或者 <span class="function"><a href="function.file-exists.html" class="function">file_exists()</a></span>
    检查目录是否已经存在。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-function.mkdir-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   目录已存在时，产生 <strong><code>E_WARNING</code></strong> 错误。
  </p>
  <p class="para">
   如果因为权限问题无法创建目录，导致 <strong><code>E_WARNING</code></strong> 错误。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mkdir-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2203">
    <p><strong>示例 #1 <span class="function"><strong>mkdir()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />mkdir</span><span style="color: #007700">(</span><span style="color: #DD0000">"/path/to/my/dir"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0700</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2204">
    <p><strong>示例 #2 通过 <code class="parameter">recursive</code> 参数使用 <span class="function"><strong>mkdir()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;期望的目录结构<br /></span><span style="color: #0000BB">$structure&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'./depth1/depth2/depth3/'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;要创建嵌套结构，必须指定&nbsp;mkdir()<br />//&nbsp;的&nbsp;$recursive&nbsp;参数。<br /><br /></span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">mkdir</span><span style="color: #007700">(</span><span style="color: #0000BB">$structure</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0777</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Failed&nbsp;to&nbsp;create&nbsp;directories...'</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;...<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mkdir-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.is-dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a> - 判断给定文件名是否是一个目录</span></li>    
    <li class="member"><span class="function"><a href="function.rmdir.html" class="function" rel="rdfs-seeAlso">rmdir()</a> - 删除目录</span></li>
    <li class="member"><span class="function"><a href="function.umask.html" class="function" rel="rdfs-seeAlso">umask()</a> - 改变当前的 umask</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>